<?php

class Log{
	const LOG_FILE = '/engine/system_data/error.log.php';
	private static $log = array();
	private static $logIncluded = false;
	public static function writeLog($error, $errLevel = E_WARNING, $file = '', $line = 0){
		if (!boolean(Config::get('errorLog'))) return ;
		
		$md5Str = md5($error . $errLevel . $file . $line);
		if (!isset(self::$log[$md5Str]) ) {
			self::$log[$md5Str] = array(
			$error ,
			$errLevel ,
			$file ,
			$line ,
			TIME
			);
			self::_writeLog(self::$log);
		}
	}
	private static function _writeLog($array){
		if (!file_exists(ROOT . self::LOG_FILE)) {
			touch(ROOT . self::LOG_FILE);
		}
		$fp = fopen(ROOT . self::LOG_FILE, 'w+');
		flock($fp, LOCK_EX);

		fwrite($fp,'<?php'.RN.'$log = ' . var_export($array,true) . ';' );

		flock($fp , LOCK_UN);
		fclose($fp);
	}
	public static function getLogAsStr(){
		self::includeLog();
		$ret = '';
		foreach (self::$log as $array){
			$ret .= 
			__('Помилка:') . ' ' . $array[0] . RN .
			__('Код помилки:') . ' ' .$array[1] .RN .
			__('Файл:') . ' ' .$array[2] .RN .
			__('Рядок:') . ' ' .$array[3] .RN .
			__('Час помилки:') . ' '. au_date('l, d.m.Y H:i',$array[4]) .RN .
			RN .'-------------------------' . RN;
		}
		return $ret;
	}
	private static function includeLog(){
		if (self::$logIncluded){
			return;
		}
		$log = array();
		include_once ROOT . self::LOG_FILE;
		self::$log = $log;
		self::$logIncluded = true;
	}
}